<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp"%>

<div class="container-fluid">
<div id="loginForm">
	<div class="form-group">
		<label>登录名</label>
    	<input type="text" name="loginName" class="form-control" check-type="required">
	</div>
	<div class="form-group">
		<label>密码</label>
    	<input type="password" name="password" class="form-control" check-type="required">
	</div>
	<div class="form-group">
		<label class="sr-only" for="password">验证码</label>
		<img src="${ctx}/captcha-image.jpg" id="captchaImage" title="看不清,点击换一张" style="float: right;"/>
		<input type="text" name="captcha" placeholder="验证码..." class="form-username form-control" check-type="required" style="width: 50%;float:left;">
		<div style="clear: both;"></div>
	</div>
	<button type="button" class="btn btn-success btn-block" id="loginBt">登&nbsp;&nbsp;录</button>
</div>
</div>
<script type="text/javascript">
$(function(){
	$("#captchaImage").click(function(){
		$(this).attr("src",'${ctx}/captcha-image.jpg?_t='+new Date().getTime());
	});
	
	var form=$('#loginForm');
	form.validation({reqmark:false});
	
	var loginName = $.cookie('loginName');
	if (loginName) {
    	form.find("input[name=loginName]").val(loginName);
    }
	
	$("#loginBt").click(function () {
        login();
    });
	form.find("input[name=password],input[name=captcha]").keydown(function(e){
    	if(e.keyCode==13){
    		login();
    	}
    });
	
	function login(){
    	var loginName = form.find("input[name=loginName]").val();
        var canRememberMe=form.find("input[name=canRememberMe]").val();
        
        if (isValid()) {
            $.cookie('loginName', loginName, {expires: 7,path : '/'});
            var datas=webUtils.formToObj(form);
            var postDataStr=JSON.stringify(datas);
			var postDataStrBase64=Base64.encode(postDataStr);
			$.post(ctx + '/jsonLogin.do',{
				code:postDataStrBase64
			},function(data){
				if (data.state==1 || data.state==4) {
					window.location.href=ctx+"/index.do";
                }else {
                	webUtils.error(data.desc);
                	form.find("input[name=password]").val("");
                	form.find("input[name=captcha]").val("");
                	$("#captchaImage").click();
                }
			},"json");
        }else{
        }
    }
    
    function isValid(){
    	return form.valid();
    }
});
</script>